package com.xiaoma.verbal.component;

import java.util.Date;
import java.util.Stack;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import com.xiaoma.verbal.dao.PrctPracticeAudioDao;
import com.xiaoma.verbal.domain.PrctPracticeAudio;

@Component
public class HandlerSucThread<T>{
	@Autowired
	private PrctPracticeAudioDao prctPracticeAudioDao;
	@Resource
	private TaskExecutor taskExecutor;
	private Stack<T> suc = new Stack<T>();

	public void putSuc(T obj) {
		synchronized (suc) {
			suc.push(obj);
		}
	}

	public void complete() {
		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (!suc.isEmpty()) {
					try {
						Integer audioId = Integer.valueOf(suc.pop().toString());
						PrctPracticeAudio audio = prctPracticeAudioDao.getById(audioId);
						if (audio != null && audio.getStatus()==1) {
							audio.setStatus(2);
							audio.setUpdateTime(new Date());
							prctPracticeAudioDao.update(audio);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}

}
